/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: discovery_proxy_factory.cpp
 */

#include <boost/asio.hpp>

#include "discovery_proxy_factory.hpp"
#include "worker_discovery_proxy.hpp"

using namespace middleware;

//-----------------------------------------------------------------------------
// Constructors and destructor
/* First option constructor */
DiscoveryProxyFactory::DiscoveryProxyFactory(WorkerDiscovery* ptrtodisc) {
	this->_proxy = 0;
	this->_ipaddr = "127.0.0.1";
	this->_port = "0";
	this->_ptrto_discovery = ptrtodisc;
}
/* Second option constructor */
DiscoveryProxyFactory::DiscoveryProxyFactory(std::string ipaddr,
		std::string port) {
	this->_proxy = 0;
	this->_ipaddr = ipaddr;
	this->_port = port;
	this->_ptrto_discovery = 0;
}
/* Destructor */
DiscoveryProxyFactory::~DiscoveryProxyFactory() {
	if (this->_proxy != 0) delete this->_proxy; /* Safe deletion */
}
//-----------------------------------------------------------------------------
// Public members
WorkerDiscoveryProxy* DiscoveryProxyFactory::getProxy() {
	if (this->_proxy != 0) delete this->_proxy; /* Clear first */
	WorkerDiscProxyConfig proxyconf;
	proxyconf.ip_to_disc = this->_ipaddr;
	proxyconf.port_to_disc = this->_port;
	proxyconf.ptrto_worker = this->_ptrto_discovery;
	this->_proxy = new WorkerDiscoveryProxy(proxyconf); /* Dynamic alloc */
	return this->_proxy;
}
